package com.wu.controller;/**
 * @author sun
 * @date 2022/3/21 - 0:18
 */

import com.wu.pojo.Categories;
import com.wu.pojo.Images;
import com.wu.pojo.Labels;
import com.wu.pojo.MaterialsValues;
import com.wu.pojo.vo.CateGoriesVo;
import com.wu.pojo.vo.MaterialsVo;
import com.wu.pojo.vo.ProductVo;
import com.wu.service.*;
import com.wu.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: TODO
 * @author
 * @date 2022/3/21 0:18
 * @version 1.0
 */
@RestController
@Api(tags="Categories模块")
@CrossOrigin
@RequestMapping("/categories")
public class CategoriesController {
    @Autowired
    private CategoriesService categoriesService;
    @Autowired
    private ImagesService imagesService;
    @Autowired
    private LabelsService labelsService;
    @Autowired
    private MaterialsService materialsService;
    @Autowired
    private MaterialsValuesService materialsValuesService;

    @PostMapping("/insertCategories")
    @ApiOperation(value="增加Categories")
    /*@Transactional(rollbackFor = Exception.class)*/
    public R insertCategories(Categories categories){
        try {
           /* categories.setId(Integer.parseInt(RandomStringUtils.randomNumeric(10)));*/
            categoriesService.insertSelective(categories);
           /* categoriesRedirectcontent.setCategoriesId(categories.getId());
            categoriesRedirectcontentService.insertSelective(categoriesRedirectcontent);*/
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }

    @PostMapping("/insertCategoriesVo")
    @ApiOperation(value="增加CategoriesVo")
    public R insertCategoriesVo(CateGoriesVo cateGoriesVo){
        try {
            if (cateGoriesVo.getProductVoList()!=null && cateGoriesVo.getProductVoList().size()>0) {
                List<ProductVo> productVoList = cateGoriesVo.getProductVoList();
                if (productVoList!=null &&productVoList.size()>0){
                productVoList.forEach(e->{
                    if (e.getImagesList()!=null &&e.getImagesList().size()>0){
                         List<Images> imagesList = e.getImagesList();
                        for (Images images : imagesList) {
                            imagesService.insertSelective(images);
                        }
                    }
                    if (e.getLabelsList()!=null&& e.getLabelsList().size()>0){
                         List<Labels> labelsList = e.getLabelsList();
                        for (Labels labels : labelsList) {
                            labelsService.insertSelective(labels);
                        }
                    }
                    if (e.getMaterialsVoList()!=null && e.getMaterialsVoList().size()>0){
                         List<MaterialsVo> materialsVoList = e.getMaterialsVoList();
                        for (MaterialsVo materialsVo : materialsVoList) {
                            materialsService.insertSelective(materialsVo);
                             List<MaterialsValues> materialsValuesList = materialsVo.getMaterialsValuesList();
                             if (materialsValuesList!=null &&materialsValuesList.size()>0){
                                 for (MaterialsValues materialsValues : materialsValuesList) {
                                     materialsValuesService.insertSelective(materialsValues);
                                 }
                             }
                        }
                    }
                });
                }
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }


    @GetMapping("/deleteCategories/{id}")
    @ApiOperation(value="删除Categories")
    /*@Transactional(rollbackFor = Exception.class)*/
    public R deleteCategories(Integer id){
        try {
            categoriesService.deleteByPrimaryKey(id);
            /*categoriesRedirectcontentService.deleteByCategoriesId(id);*/
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @PutMapping("/updateCategories")
    @ApiOperation(value="更新Categories")
    /*@Transactional(rollbackFor = Exception.class)*/
    public R updateCategories(@RequestBody Categories categories){
        try {
            categoriesService.updateByPrimaryKeySelective(categories);
           /* categoriesRedirectcontentService.updateByPrimaryKeySelective(categoriesRedirectcontent);*/
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findCategoriesList")
    @ApiOperation(value="查询CategoriesList")
    public R findCategories(Categories categories){
        try {
            List<CateGoriesVo> list= categoriesService.findCategoriesVo(categories);
            return R.ok().data("categoriesList",list);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findCategoriesById/{id}")
    @ApiOperation(value="通过id查询Categories")
    public R findCategoriesById(Integer id){
        try {
            Categories categories= categoriesService.selectByPrimaryKey(id);
            return R.ok().data("categories",categories);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

}
